package com.kmust.dream.wanderingdream.shoppingcart.controller;

import com.kmust.dream.wanderingdream.comm.pojo.ResultBean;
import com.kmust.dream.wanderingdream.shoppingcart.entity.ShoppingCartItem;
import com.kmust.dream.wanderingdream.shoppingcart.feign.ProductSkuFeign;
import com.kmust.dream.wanderingdream.shoppingcart.service.ShoppingCartItemService;
import com.kmust.dream.wanderingdream.shoppingcart.utils.ReturnUtils;
import com.kmust.dream.wanderingdream.shoppingcart.vo.ItemNumberVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/shop/sciNumber")
public class ShoppingCartItemChangeController {


    @Autowired
    private ShoppingCartItemService ShoppingCartItemService;
    @Autowired
    private ProductSkuFeign productSkuFeign;


    /**
     * 更新item的数量
     * @param itemNumberVO 数据传输类
     * @return resultBean
     */
    @ApiOperation("更新购物车商品数量")
    @PutMapping("/updateItemNumber")
    public ResultBean updateItemNumber(@RequestBody ItemNumberVO itemNumberVO) {
        Integer itemId = itemNumberVO.getItemId();
        ShoppingCartItem byId = ShoppingCartItemService.getById(itemId);
        Integer skuId = byId.getProductSkuId();
        Integer flagNumber = itemNumberVO.getFlagNumber();
        ResultBean skuStock = getSkuStock(skuId);
        if (ReturnUtils.parseResult(skuStock)) {
            Integer stock = (Integer) skuStock.getPayload();
            return ShoppingCartItemService.updateItemNumber(itemId, flagNumber, stock);
        }
        return skuStock;
    }

    /**
     * 获取stock
     * @param skuId productSku的主键
     * @return resultBean
     */
    @ApiOperation("获取skuStock")
    @GetMapping("/getSkuStock/{skuId}")
    public ResultBean getSkuStock(@PathVariable Integer skuId) {
        return productSkuFeign.getSkuStock(skuId);
    }
}
